def prefix(m, nums):
    # 找到输入数据中的最大坐标
    max_i = max(num[0] for num in nums)
    max_j = max(num[1] for num in nums)
    
    # 给数组赋值
    prefix_array = [[0] * (max_j + 2) for _ in range(max_i + 2)]
    for num in nums:
        i, j, value = num
        prefix_array[i + 1][j + 1] += value
    
    # 计算前缀和
    for i in range(1, max_i + 2):
        for j in range(1, max_j + 2):
            prefix_array[i][j] += prefix_array[i - 1][j] + prefix_array[i][j - 1] - prefix_array[i - 1][j - 1]
    
    # 计算爆炸范围
    max_value = 0
    # 遍历地图，根据炸弹边长m确定每个正方形覆盖区域并计算其价值总和
    for i in range(1, max_i + 1 - m):
        for j in range(1, max_j + 1 - m):
        # 利用前缀和数组计算当前正方形区域（边长为m）内的价值总和
            current_value = prefix_array[i + m][j + m] - prefix_array[i + m][j - 1] - prefix_array[i - 1][j + m] + prefix_array[i - 1][j - 1]
            max_value = max(max_value, current_value)
    return max_value

n, m = map(int, input().split())
nums = [list(map(int, input().split())) for _ in range(n)]
result = prefix(m, nums)
print(result)

